package com.jast.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jast.common.utils.DateUtils;
import com.jast.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.jast.common.annotation.Log;
import com.jast.common.enums.BusinessType;
import com.jast.business.domain.BigdataRedisKv;
import com.jast.business.service.IBigdataRedisKvService;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.poi.ExcelUtil;
import com.jast.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * redis 监控实时指标Controller
 * 
 * @author Jast
 * @date 2023-01-31
 */
@Controller
@RequestMapping("/business/redis")
public class BigdataRedisKvController extends BaseController
{
    private String prefix = "business/redis";

    @Autowired
    private IBigdataRedisKvService bigdataRedisKvService;

    @RequiresPermissions("business:redis:view")
    @GetMapping()
    public String redis()
    {
        return prefix + "/redis";
    }

    /**
     * 查询redis 监控实时指标列表
     */
    @RequiresPermissions("business:redis:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BigdataRedisKv bigdataRedisKv)
    {
        startPage();
        List<BigdataRedisKv> list = bigdataRedisKvService.selectBigdataRedisKvList(bigdataRedisKv);
        return getDataTable(list);
    }

    /**
     * 导出redis 监控实时指标列表
     */
    @RequiresPermissions("business:redis:export")
    @Log(title = "redis 监控实时指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BigdataRedisKv bigdataRedisKv)
    {
        List<BigdataRedisKv> list = bigdataRedisKvService.selectBigdataRedisKvList(bigdataRedisKv);
        ExcelUtil<BigdataRedisKv> util = new ExcelUtil<BigdataRedisKv>(BigdataRedisKv.class);
        return util.exportExcel(list, "redis 监控实时指标数据");
    }

    /**
     * 新增redis 监控实时指标
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存redis 监控实时指标
     */
    @RequiresPermissions("business:redis:add")
    @Log(title = "redis 监控实时指标", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BigdataRedisKv bigdataRedisKv)
    {
        return toAjax(bigdataRedisKvService.insertBigdataRedisKv(bigdataRedisKv));
    }

    /**
     * 修改redis 监控实时指标
     */
    @RequiresPermissions("business:redis:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BigdataRedisKv bigdataRedisKv = bigdataRedisKvService.selectBigdataRedisKvById(id);
        mmap.put("bigdataRedisKv", bigdataRedisKv);
        return prefix + "/edit";
    }

    /**
     * 修改保存redis 监控实时指标
     */
    @RequiresPermissions("business:redis:edit")
    @Log(title = "redis 监控实时指标", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BigdataRedisKv bigdataRedisKv)
    {
        return toAjax(bigdataRedisKvService.updateBigdataRedisKv(bigdataRedisKv));
    }

    /**
     * 修改保存redis 监控实时指标
     */
    @Log(title = "redis 监控实时指标", businessType = BusinessType.UPDATE)
    @PostMapping("/upsert")
    @ResponseBody
    public AjaxResult upsert(BigdataRedisKv bigdataRedisKv)
    {
        return toAjax(bigdataRedisKvService.updateBigdataRedisKvByKey(bigdataRedisKv));
    }

    /**
     * 删除redis 监控实时指标
     */
    @RequiresPermissions("business:redis:remove")
    @Log(title = "redis 监控实时指标", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bigdataRedisKvService.deleteBigdataRedisKvByIds(ids));
    }


    @GetMapping("/monitor")
    public String redis(ModelMap modelMap, HttpServletRequest request)
    {
        HashMap<String, String> map = new HashMap<>();
        modelMap.put("redis",map);
        BigdataRedisKv bigdataRedisKv = new BigdataRedisKv();
        List<BigdataRedisKv> list = bigdataRedisKvService.selectBigdataRedisKvList(bigdataRedisKv);

        for (BigdataRedisKv redisKv : list) {
            map.put(redisKv.getRkey(),redisKv.getRvalue());
            map.put(redisKv.getRkey()+"_time", redisKv.getUpdateTime().toLocaleString());
        }

        return "bigdata/redis/redis";
    }
}
